using System;
using System.Collections.Generic;
using System.Text;
using Index.PostingLists;
using Index.PostingLists.Entry;

namespace Query
{
    public class Sum
    {


        public static List<int> sum(DocumentPositionList[] lists)
        {
            List<int> result = new List<int>();
            int[] indices = new int[lists.Length];
            Boolean[] cleared = new Boolean[lists.Length];

            for (int i = 0; i < indices.Length; i++)
                indices[i] = 0;
            

            for (int i = 0; i < cleared.Length; i++)
                cleared[i] = false;
            
            int clearedListCounter = 0;
            int minId, minIndex;

            while (clearedListCounter != lists.Length)
            {
                minId = int.MaxValue;
                minIndex = int.MaxValue;

                for (int i = 0; i < lists.Length; i++)
                    if (!cleared[i] && lists[i][indices[i]].DocumentId == minId)
                    {
                        // oznacza ze jest pare z tym samym id i tzreba sie przesunac
                        indices[i]++;
                        if (indices[i] == lists[i].Count)
                        {
                            cleared[i] = true;
                            clearedListCounter++;
                        }
                    }
                    else if (!cleared[i] && lists[i][indices[i]].DocumentId < minId)
                    {
                        minId = lists[i][indices[i]].DocumentId;
                        minIndex = i;
                    }

                result.Add(minId);
                indices[minIndex]++;
                if (indices[minIndex] == lists[minIndex].Count)
                {
                    cleared[minIndex] = true;
                    clearedListCounter++;
                }
                
            }

            return result;
        }

        // glupie rozwiazanie ale przekopiowane to co wyzej
        // 
        public static List<int> sum(DocumentFrequencyList[] lists)
        {
            List<int> result = new List<int>();
            int[] indices = new int[lists.Length];
            Boolean[] cleared = new Boolean[lists.Length];

            for (int i = 0; i < indices.Length; i++)
                indices[i] = 0;


            for (int i = 0; i < cleared.Length; i++)
                cleared[i] = false;

            int clearedListCounter = 0;
            int minId, minIndex;

            while (clearedListCounter != lists.Length)
            {
                minId = int.MaxValue;
                minIndex = int.MaxValue;

                for (int i = 0; i < lists.Length; i++)
                    if (!cleared[i] && lists[i][indices[i]].DocumentId == minId)
                    {
                        // oznacza ze jest pare z tym samym id i tzreba sie przesunac
                        indices[i]++;
                        if (indices[i] == lists[i].Count)
                        {
                            cleared[i] = true;
                            clearedListCounter++;
                        }
                    }
                    else if (!cleared[i] && lists[i][indices[i]].DocumentId < minId)
                    {
                        minId = lists[i][indices[i]].DocumentId;
                        minIndex = i;
                    }

                result.Add(minId);
                indices[minIndex]++;
                if (indices[minIndex] == lists[minIndex].Count)
                {
                    cleared[minIndex] = true;
                    clearedListCounter++;
                }

            }

            return result;

        }


        public static DocumentFrequencyList convertFreq(List<int> list)
        {
            DocumentFrequencyList result = new DocumentFrequencyList();
            foreach (int x in list)
            {
                result.Add(new FrequencyEntry(x, 1));
            }
            return result;
        }

        public static DocumentPositionList convertPost(List<int> list)
        {
            DocumentPositionList result = new DocumentPositionList();
            foreach (int x in list)
            {
                result.Add(new PositionEntry(x, null));
            }
            return result;
        }


        public static List<int> convert(DocumentPositionList list)
        {
            List<int> result = new List<int>();
            foreach (PositionEntry entry in list)
                result.Add(entry.DocumentId);
            return result;
        }

        public static List<int> convert(DocumentFrequencyList list)
        {
            List<int> result = new List<int>();
            foreach (FrequencyEntry entry in list)
                result.Add(entry.DocumentId);
            return result;
        }



    }
}
